import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:shop/common/common.dart';
import 'package:shop/common/widget/sms_counter_view.dart';
import 'package:shop/common/widget/textfield_view.dart';
import 'package:shop/login/widget/buttom_button.dart';

class ForgetPwdPage extends StatefulWidget {
  @override
  _ForgetPwdPageState createState() => _ForgetPwdPageState();
}

class _ForgetPwdPageState extends State<ForgetPwdPage> {
  final _focusNode = FocusNode();
  final _controller = TextEditingController();
  final _codefocusNode = FocusNode();
  final _codeController = TextEditingController();
  String phone = "";
  @override
  void dispose() {
    _controller.dispose();
    _codeController.dispose();
    super.dispose();
  }

  void _jumpSmsCode() {
    if (_controller.text.length != 11) {
      EasyLoading.showError("请输入11位手机号码");
      return;
    }
    if (_codeController.text.length < 6) {
      EasyLoading.showError("请输入6位验证码");
      return;
    }
    Navigator.pushNamed(context, "/resetpwd", arguments: {'phone': _controller.text, "code": _codeController.text});
  }

  @override
  Widget build(BuildContext context) {
    final size = MediaQuery.of(context).size;
    final marginH = size.height * 0.06;

    return Scaffold(
      backgroundColor: Colors.white,
      body: GestureDetector(
        behavior: HitTestBehavior.translucent,
        onTap: () => _focusNode.unfocus(),
        child: SafeArea(
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: <Widget>[
              IconButton(
                icon: Icon(Icons.arrow_back_ios),
                onPressed: () => Navigator.pop(context),
              ),
              SizedBox(height: marginH),
              Padding(
                padding: const EdgeInsets.all(20),
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: <Widget>[
                    Text(
                      "找回密码",
                      style: AppFont.textStyle(25, color: AppColor.font333, fontWeight: FontWeight.bold),
                    ),
                    SizedBox(height: 10),
                    SizedBox(height: marginH),
                    NormalTextfield(
                      hintText: "请输入手机号码",
                      backgroundColor: Colors.white,
                      inputFormatters: [FilteringTextInputFormatter.digitsOnly],
                      keyboardType: TextInputType.number,
                      focusNode: _focusNode,
                      maxLength: 11,
                      controller: _controller,
                      onSubmited: (val) {
                        setState(() {
                          phone = val;
                        });
                      },
                      onChanged: (val) {
                        print(val);
                        setState(() {
                          phone = val;
                        });
                      },
                    ),
                    SizedBox(height: 1, child: Divider()),
                    SizedBox(
                      height: 8,
                    ),
                    Row(
                      children: [
                        Expanded(
                          child: NormalTextfield(
                            hintText: "请输入验证码",
                            backgroundColor: Colors.white,
                            maxLength: 6,
                            inputFormatters: [FilteringTextInputFormatter.digitsOnly],
                            keyboardType: TextInputType.number,
                            focusNode: _codefocusNode,
                            controller: _codeController,
                          ),
                        ),
                        SmsCounterView(
                          phone: phone,
                        )
                      ],
                    ),
                    SizedBox(height: 1, child: Divider()),
                    SizedBox(height: marginH),
                    BottomButton(
                      margin: const EdgeInsets.only(top: 40),
                      title: "下一步",
                      onTap: () => _jumpSmsCode(),
                    ),
                  ],
                ),
              )
            ],
          ),
        ),
      ),
    );
  }
}
